package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.*;
import com.carol.lmsbe.service.EquipmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 设备管理控制器
 */
@RestController
@RequestMapping("/api/equipment")
@RequiredArgsConstructor
@Slf4j
public class EquipmentController {

    private final EquipmentService equipmentService;

    /**
     * 分页查询设备列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentDTO>> listEquipment(EquipmentQueryRequest request) {
        try {
            Page<EquipmentDTO> result = equipmentService.listEquipment(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询设备列表失败", e);
            return Result.error("查询设备列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取设备详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentDTO> getEquipmentById(@PathVariable Long id) {
        try {
            EquipmentDTO result = equipmentService.getEquipmentById(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备详情失败", e);
            return Result.error("获取设备详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建设备
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<EquipmentDTO> createEquipment(@Valid @RequestBody EquipmentCreateRequest request) {
        try {
            EquipmentDTO result = equipmentService.createEquipment(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建设备失败", e);
            return Result.error("创建设备失败: " + e.getMessage());
        }
    }

    /**
     * 更新设备
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<EquipmentDTO> updateEquipment(@PathVariable Long id, @Valid @RequestBody EquipmentUpdateRequest request) {
        try {
            request.setId(id);
            EquipmentDTO result = equipmentService.updateEquipment(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新设备失败", e);
            return Result.error("更新设备失败: " + e.getMessage());
        }
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> deleteEquipment(@PathVariable Long id) {
        try {
            equipmentService.deleteEquipment(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除设备失败", e);
            return Result.error("删除设备失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除设备
     */
    @DeleteMapping("/batch")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> batchDeleteEquipment(@RequestBody List<Long> ids) {
        try {
            equipmentService.batchDeleteEquipment(ids);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除设备失败", e);
            return Result.error("批量删除设备失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备统计信息
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<EquipmentStatisticsResponse> getEquipmentStatistics(EquipmentStatisticsRequest request) {
        try {
            EquipmentStatisticsResponse result = equipmentService.getEquipmentStatistics(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备统计信息失败", e);
            return Result.error("获取设备统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有设备类别
     */
    @GetMapping("/categories")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<List<String>> getAllCategories() {
        try {
            List<String> result = equipmentService.getAllCategories();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备类别失败", e);
            return Result.error("获取设备类别失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有设备状态
     */
    @GetMapping("/statuses")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<List<String>> getAllStatuses() {
        try {
            List<String> result = equipmentService.getAllStatuses();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备状态失败", e);
            return Result.error("获取设备状态失败: " + e.getMessage());
        }
    }

    /**
     * 导出设备数据
     */
    @GetMapping("/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<byte[]> exportEquipmentData(EquipmentQueryRequest request) {
        try {
            byte[] result = equipmentService.exportEquipmentData(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("导出设备数据失败", e);
            return Result.error("导出设备数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取可用设备列表
     */
    @GetMapping("/available")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<List<EquipmentDTO>> getAvailableEquipments() {
        try {
            List<EquipmentDTO> result = equipmentService.getAvailableEquipments();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取可用设备列表失败", e);
            return Result.error("获取可用设备列表失败: " + e.getMessage());
        }
    }
}
